package com.chen.nio;

import java.nio.ByteBuffer;

/**
 * sliceBuffer和原buffer共享一个底层数组
 */
public class NioTest5 {

    public static void main(String[] args) {

        ByteBuffer buffer = ByteBuffer.allocate(10);
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }
        buffer.position(2);
        buffer.limit(6);

        ByteBuffer sliceBuffer = buffer.slice();
        for (int i = 0; i < sliceBuffer.capacity(); i++) {
            byte b = sliceBuffer.get(i);
            b *= 2;
            sliceBuffer.put(b);
        }
        sliceBuffer.flip();
        while( sliceBuffer.hasRemaining() ){
            System.out.print(sliceBuffer.get() + ",");
        }
        System.out.println();
        buffer.position(0);
        buffer.limit(buffer.capacity());
        while( buffer.hasRemaining() ){
            System.out.print(buffer.get() + ",");
        }
    }
}
